@extends('layouts.back_stage')
@section('content')
    <style>
        .lining-btn{
            clear: both;
            margin-top: 10px;
        }
        .container .lining-btn .lining-a{
            float:left;
        }
        .container .lining-btn .active_color{
            background-color:#27c24c;
        }
        .info_box{
            width:100%;
            height:30px;
            margin-top:20px;
        }
        .info_box li{
            width:20%;
            float:left;
            height:30px;
        }
        .info_box li h3{
            display: inline;
        }
        .info_box li h3 input{
            display: inline-block;
            width:100px;
            border-top:none;
            border-left:none;
            border-right:none;
            background: none;
        }
        .container_color_box{
            width:100%;
        }
        .container_color_box li{
            display: none;
            width:100%;
        }
        .pantone input{
            border:none;
            text-align: center;
        }
        .audit{
            float:right;
            margin-right:10%;
            margin-top:10px;
        }
        .container_color_box .pantone{
            width:80%;
            margin:0 auto;
        }
        .container_color_box h3{
            padding-left:10%;
            padding-top:40px;
            padding-bottom: 20px;
        }
        .save-size{
            float:right;
            margin-right:10%;
            margin-top:10px;
        }
        .ship_date{
            width:150px;
        }
        .factory{
            width: 45%;
        }
    </style>
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/clothInfo.css"/>
    <link href="{{env('CDN_DOMAIN_NAME')}}/css/jcDate.css" rel="stylesheet">
    <div class="container">
        <div><ul class="info_box">
                <li>
                    <h3 class="getGirard">款号: <span>{{ $investment->girard }}</span></h3>
                </li>
                <li>
                    <h3 class="plant_code">计划单号:
                        <span>
                            @if(strlen($investment->id) == 1)
                                T000000{{ $investment->id }}
                            @elseif(strlen($investment->id) == 2 )
                                T00000{{ $investment->id }}
                            @elseif(strlen($investment->id) == 3 )
                                T0000{{ $investment->id }}
                            @elseif(strlen($investment->id) == 4 )
                                T000{{ $investment->id }}
                            @elseif(strlen($investment->id) == 5 )
                                T00{{ $investment->id }}
                            @elseif(strlen($investment->id) == 6 )
                                T0{{ $investment->id }}
                            @elseif(strlen($investment->id) == 7 )
                                T{{ $investment->id }}
                            @endif
                        </span>
                    </h3>
                </li>
                <li>
                    <h3>波段:{{ $investment->band_name }}</h3>
                </li>
                <li>
                    <h3>品类:{{ $category[$investment->cate_id] }}</h3>
                </li>
                <li>
                    <h3>投产总量:<input type="text" class="form-control gross" value="{{$investment->total or ''}}"/></h3>
                </li>
            </ul>
            <ul class="info_box">
                <li>
                    <h3>剩余量:<input type="text" class="form-control surplus" placeholder="由系统计算"  name="into_pur" value="{{$investment->into_pur or '0'}}"/></h3>
                </li>
                <li>
                    <h3>计划加工厂:
                        <select name="plan_factory" id="" class="address row_sel row_right factory">
                                <option value="">请选择</option>
                            @foreach($suppliers as $k =>$v)
                                <option value="{{$v->name}}"
                                @if($investment->plan_factory==$v->name)
                                    selected="selected"
                                @endif
                                >{{$v->name}}</option>
                            @endforeach
                        </select>
                    </h3>
                </li>
                <li>
                    <h3>计划投产日期:<input class="jcDate form-control mark_date" id="purchase_date"  style="width: 100px;display: inline-block;" placeholder="投产日期" autocomplete="off" value="{{$investment->investment_at or ''}}"  /></h3>
                </li>
                <li>
                    <h3>计划出货日期:<input class="jcDate form-control ship_date" id="purchase_date"  style="width: 100px;display: inline-block;" placeholder="出货日期" autocomplete="off" value="{{$investment->plan_date or ''}}"  /></h3>
                </li>
                <li></li>
            </ul></div>
        <div class="lining-btn">
            @if(count($goods) >0 )
                @foreach($goods as $k=>$v)
            <a class="lining-a" id="l{{ $v->id }}">{{ $v->color }}</a>
                @endforeach
                @endif
        </div>
        <ul class="container_color_box">
            @if(count($goods) >0 )
                @foreach($goods as $k=>$val)
            <li id="M{{ $val->id }}">
                <h3>主产尺码</h3>
                <table class="pantone" border="1" cellspacing="0" cellpadding="0">
                    <thead>
                    <tr>
                        <td class="col-md-1">尺码:</td>
                        <?php
                            $size = explode(',',$val->size);
                        ?>
                        @foreach($size as $v)
                        <td class="col-md-1">{{ $v }}</td>
                        @endforeach
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <?php
                        $size_stock = explode(',',$val->stock);
                        ?>
                        <td>件数:</td>
                        @if(count($size)>0 && $val->stock=='')
                            @for($i=0;$i<count($size);$i++)
                                  <td><input type="text" class="form-control getSizeNum"></td>
                            @endfor
                        @else
                            @foreach($size_stock as $vv)
                                <td><input type="text" class="form-control getSizeNum" value="{{$vv}}"/></td>
                            @endforeach
                        @endif
                    </tr>
                    </tbody>
                </table>
                <button class="btn btn-success save-size"
                    @if($investment->status !=1 && $investment->status!=0)
                        disabled>提交</button>
                    @else >提交</button>
                    @endif

                <h3>所需面料</h3>
                <table class="pantone1 pantone" border="1" cellspacing="0" cellpadding="0">
                    <thead>
                    <tr>
                        <td class="col-md-1">面料号</td>
                        <td class="col-md-1">颜色</td>
                        <td class="col-md-1">库存</td>
                        <td class="col-md-1">用量</td>
                        <td class="col-md-1">操作</td>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($details as $ks=>$vs)
                        @if($val->id == $vs->product_id)
                        <tr>
                            <td class="sur_cloth_info">{{ $vs->cloth_info }}</td>
                            <td class="sur_cloth_color">{{ $vs->other_info }}</td>
                            <td class="sur_now_stock">{{ $vs->stocks  }}</td>
                            <td class="sur_need_stock"><input type="text" class="form-control" value="{{$vs->actual_need_stock}}"></td>
                            <td><button class="btn btn-info save_surface" id="{{ $vs->product_id }}"
                                @if(($vs->actual_need_stock>0 || $investment->status !=1) && $investment->status!=0)
                                    disabled="disabled">已保存</button>
                                @else
                                    >保存</button>
                                @endif
                            </td>
                        </tr>
                        @endif
                    @endforeach
                    </tbody>
                </table>
                        @if($investment->status==2)
                            <button class="btn btn-success audit"  id="{{ $vs->product_id }}">核料</button>
                        @elseif($investment->status==0 || $investment->status==1)
                            <button class="btn btn-info" onclick="saveInvestment()" style="float:right;margin-right:10%;margin-top:10px;">保存</button>
                        @elseif($investment->status==3)
                            <button class="btn btn-success audit"  id="{{ $vs->product_id }}">投产</button>
                        @endif
                </li>
                @endforeach
            @endif
        </ul>
        <a class="btn btn-info" style="clear:both;margin-top:15px;margin-right:10%;float:right;" href="/admin/operation/put_allPlan">返回上一页</a>
    </div>
    <script type="text/javascript">
        //页面初始化添加选项卡样式
        $(document).ready(function () {
            $('.container_color_box li:first').show();
            $('.container_color_box li:first').addClass('active_li');
            $('.lining-a:first').addClass('active_color');
        });
        //日期选择器
        $(function (){
            $(".jcDate").jcDate({
                IcoClass : "jcDateIco",
                Event : "click",
                Speed : 100,
                Left : 0,
                Top : 28,
                format : "-",
                Timeout : 5000
            });
        });
        //判断实际需求库存是否超过投产总量
        function checkSurplus() {
            var surplus = Number(0);
            $('.getSizeNum').each(function () {
                surplus +=+ $(this).val();
            });
            $('.surplus').val($('.gross').val()-surplus);
            if($('.gross').val()-surplus <0){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text('主产尺码数量超过现有库存！请修改！');
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
        }
        //改变尺码件数判断是否超过投产总量
        $(document).on('change','.getSizeNum',function () {
            checkSurplus();
        });
        (function ($) {     //获取URL参数
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        })(jQuery);
        //点击切换选项卡
        $('.lining-a').click(function () {
            var that_ = $(this);
            that_.addClass('active_color');
            that_.siblings().removeClass('active_color');
            var id = that_.attr('id').replace(/[^0-9]/ig,"");
            for(var i=0; i< $(".container_color_box li").length;i++){
                var liID = $(".container_color_box li:eq("+[i]+")").attr('id').replace(/[^0-9]/ig,"");
                if(liID === id){
                    $(".container_color_box li:eq("+[i]+")").show();
                    $(".container_color_box li:eq("+[i]+")").addClass('active_li');
                    $(".container_color_box li:eq("+[i]+")").siblings().hide();
                    $(".container_color_box li:eq("+[i]+")").siblings().removeClass('active_li');
                }
            }
        });
        //保存所需面料
        $('.save_surface').click(function () {
            var that_ = $(this);
            var getClothCode = that_.parent().siblings('.sur_cloth_info').text();
            var getColorInfo = that_.parent().siblings('.sur_cloth_color').text();
            var getNeedStock = that_.parent().siblings('.sur_need_stock').children().val();
            var getClothID = that_.attr('id');
            var getNowStock = that_.parent().siblings('.sur_now_stock').text();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type: 'post',
                url: '/production/surplus_stock',
                dataType:'json',
                data:{
                    cloth_code:getClothCode,
                    other_info:getColorInfo,
                    actual_need_stock:getNeedStock,
                    id:getClothID
                },
                success:function (res) {
                    if(res.success){
                        that_.text('已保存');
                        that_.attr('disabled','disabled')
                    }
                }
            })
        });
        //保存上方信息
        function saveInvestment() {
            if($('.gross').val()=='' || $('.factory').val() == '' || $('.ship_date').val() == '' || $('.mark_date').val() == ''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请填写完整");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type: 'post',
                url: '/production/saveInvestment',
                dataType:'json',
                data: {
                    total:$('.gross').val(),
                    plan_code:$('.plant_code span').text().replace(/\s/g, ""),
                    into_pur:$('.surplus').val(),
                    plan_factory:$('.factory').val(),
                    plan_date:$('.ship_date').val(),
                    investment_at:$('.mark_date').val(),
                    id:$.getUrlParam('id')
                },
                success:function (event) {
                    if(event.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("保存成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        }
        //保存主产尺码
        $('.save-size').click(function () {
            var color = $('.active_color').text();
            var girard = $('.getGirard span').text();
            var sizeArray = "";
            $('.container_color_box li.active_li .getSizeNum').each(function () {
                if($(this).val() == ''){
                    sizeArray += 0+',';
                }else {
                    sizeArray += $(this).val()+',';
                }
            });
            checkSurplus();
            var surplus = Number(0);
            $('.getSizeNum').each(function () {
                surplus +=+ $(this).val();
            });
            if($('.gross').val()-surplus <0){
                return false;
            }
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type: 'post',
                url: '/production/size_save',
                dataType:'json',
                data:{
                    color:color,
                    girard:girard,
                    size:sizeArray
                },
                success:function (event) {
                    if(event.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text(event.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(event.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });
        //下方核料或投产按钮
        $('.audit').click(function () {
            var that_ = $(this)
            var colorId = $('.active_color').attr('id').replace(/[^0-9]/ig,"");
            var numArray = "";
            var girard = $('.getGirard span').text();
            $('.container_color_box li.active_li .sur_now_stock').each(function () {
                numArray += $(this).text()+',';
            });
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type: 'post',
                url: '/production/check_stock',
                dataType:'json',
                data:{
                    product_id:colorId,
                    stock:numArray,
                    investment_id:$.getUrlParam('id'),
                    girard:girard
                },
                success:function (event) {
                    if(event.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text($('.audit').text()+'成功!');
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        that_.attr('disabled','disabled');
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(event.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        })

    </script>
@endsection
@section('script')
    @parent
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/jQuery-jcDate.js" type="text/javascript"></script>
    <script src="https://www.jq22.com/jquery/jquery-migrate-1.2.1.min.js"></script><!--JQ的版本引导-->
@endsection